#!/usr/bin/perl -w
use warnings;
use strict;
use DBI;
require('config.pl');
require('function.pl');

my $db_conn = DBI->connect("DBI:mysql:database=58_spider;host=$ENV{'db_host'}", $ENV{'db_user'}, $ENV{'db_password'});
$db_conn->do('set names utf8');
my $min_id = execute_scalar('select min(id) from spider_data', $db_conn);
my $max_id = execute_scalar('select max(id) from spider_data', $db_conn);
for (my $id = $min_id; $id <= $max_id; ++$id) {
	my $url = execute_scalar("select keyword4 from spider_data where id = $id and good = 0", $db_conn);
	if ($url =~ /v=(\w+)/) {
		my $valid = 2;
		my $phone_num = 0;
		my $code = $1;
		my $html = `curl -s 'http://192.168.100.237/Ocr58Service.aspx?UserName=jiaohou&Password=jiaohou&image=$code'`;
		if ($html =~ /<div>\s*(\d{11})\s*<\/div>/) {
			$phone_num = $1;
			if (execute_scalar("select count(*) from spider_data where phone_num = '$phone_num'", $db_conn) == 0) {
				$valid = 1;
			}
		}
		$db_conn->do("update spider_data set phone_num = '$phone_num', good = $valid where id = $id");
		print "$id\t$phone_num\t$valid\n";
	}
}
